$(document).ready(function(){
	DatabaseSearch.init();
	
	var isforeign = $("#hidden_isforeign").val();
	
	var data = {f:isforeign};
	
	$(":input[name='f'][value='"+isforeign+"']").attr("checked", true);
	DatabaseSearch.find(data);
});
DatabaseSearch={};
DatabaseSearch.init = function(){
	$("#subjects_all").click(function(){
		$(":input[name='subjects']").attr("checked",true);
		$("#subjects_notall").attr("checked",false);
	});

	$("#subjects_notall").click(function(){
		$(":input[name='subjects']").attr("checked",false);
		$("#subjects_all").attr("checked",false);
	});
	
	$("#contentTypes_all").click(function(){
		$(":input[name='contentTypes']").attr("checked",true);
		$("#contentTypes_notall").attr("checked",false);
	});

	$("#contentTypes_notall").click(function(){
		$(":input[name='contentTypes']").attr("checked",false);
		$("#contentTypes_all").attr("checked",false);
	});
	
	$("#initial_all").click(function(){
		$(":input[name='initial']").attr("checked",true);
		$("#initial_notall").attr("checked",false);
	});
	
	$("#initial_notall").click(function(){
		$(":input[name='initial']").attr("checked",false);
		$("#initial_all").attr("checked",false);
	});
	
	$("#do-search").on("click", function(e){
		
		var url = "database_finds.jspx?action=search&format=json";
		var options = {
				url:url,
				success:function(data){
					DatabaseSearch.parseResult(data);
				}};
		$("#databases_search_form").ajaxSubmit(options);
	});
};

DatabaseSearch.parseResult = function(result){
	var databases = result.databases;
	if(databases!==undefined){
		var databases_body = $("#databases_body");
		databases_body.empty();
		
		$.each(databases,function(index,element){
			var tdata = {};
			tdata.index = index + 1;
			if(element.isnet===0){
				tdata.isnet = "镜像";
			}else{
				tdata.isnet = "网络";
			}
			tdata.database = element;
			var template_html = template("database-body-template",tdata);
			databases_body.append(template_html);
		});
	}
};

DatabaseSearch.find = function(data){
	var url = "database_finds.jspx?action=search&format=json";
	$.get(url, data, function(result) {
		DatabaseSearch.parseResult(result);
	});
};
